package com.stone.im.dispatcher;

import com.stone.im.common.Constants;
import com.stone.im.protobuf.proto.AuthenticateRequestProto;
import com.stone.im.protobuf.proto.AuthenticateResponseProto;

/**
 * 请求处理组件
 */
public class RequestHandler {

    static class Singleton{
        static RequestHandler instance = new RequestHandler();
    }

    public static RequestHandler getInstance(){
        return Singleton.instance;
    }

    /**
     * 处理认证请求
     * @param authenticateRequest
     * @return
     */
    public AuthenticateResponseProto.AuthenticateResponse authenticate(AuthenticateRequestProto.AuthenticateRequest authenticateRequest){

        System.out.println("处理认证请求");
        //请求sso单点登录系统判断是否登录的用户
        AuthenticateResponseProto.AuthenticateResponse.Builder builder = AuthenticateResponseProto.AuthenticateResponse.newBuilder();
        try {
            String uid = authenticateRequest.getUid();
            String token = authenticateRequest.getToken();
            //认证成功
            if (authenticateBySSO(uid,token)){
                builder.setErrorMessage(Constants.RESPONSE_ERROR_MESSAGE_EMPTY);
                builder.setStatus(Constants.RESPONSE_STATUS_OK);
                builder.setErrorCode(Constants.RESPONSE_ERROR_CODE_UNKNOWN);
            }else {
                builder.setErrorMessage(Constants.RESPONSE_ERROR_MESSAGE_EMPTY);
                builder.setStatus(Constants.RESPONSE_STATUS_ERROR);
                builder.setErrorCode(Constants.RESPONSE_ERROR_CODE_AUTHENTICATE_FAILURE);
            }
        }catch (Exception e){
            builder.setErrorMessage(e.toString());
            builder.setStatus(Constants.RESPONSE_STATUS_ERROR);
            builder.setErrorCode(Constants.RESPONSE_ERROR_CODE_AUTHENTICATE_EXCEPTION);
        }
        AuthenticateResponseProto.AuthenticateResponse build = builder.build();
        System.out.println("已经向sso单点登录系统认证完毕");
        return build;
    }

    /**
     * 单点登录认证
     * @param uid
     * @param token
     * @return
     */
    public boolean authenticateBySSO(String uid , String token){

        return true;
    }
}
